package com.zwl.smartplatform.controller.advices;

import com.zwl.smartplatform.dto.R;
import com.zwl.smartplatform.exceptions.MyBaseException;
import com.zwl.smartplatform.exceptions.ResultCode;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice//声明当前类专门拦截controller 这个注解是spring实现的
@Component//由spring创建对象
@ResponseBody
public class  MyControllerAdvice {

    @ExceptionHandler(MyBaseException.class)//异常处理器 声明当前注解是专门处理MyBaseException类型的异常
    public R processMyBaseException(MyBaseException e){
        int code=e.getCode();
        String message = e.getMessage();
        return R.setError(code, message);

    }

    //我们发现 除了上面到异常外 我们也会有未知到异常
    @ExceptionHandler(Exception.class)
    public R processMyBaseException(Exception e){
        return R.setError(ResultCode.FAIL,"请检查你到网络",null);
    }
}
